package org.apache.struts.pharmacy.pharmacy.action;

import java.util.Date;
import java.util.Map;

import org.apache.struts.pharmacy.pharmacy.model.HL7Message;
import org.apache.struts.pharmacy.pharmacy.model.ReleaseDrugs;

import com.earogya.communicate.Communicator;
import com.earogya.logic.pharmacy.release.CreateRDSO13;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ReleaseDrugsAction extends ActionSupport{
	private ReleaseDrugs releaseDrugs;
	private HL7Message message;
	private Date date;

	public HL7Message getMessage() {
		return message;
	}

	public String execute() throws Exception {
		Map<String, Object> session=ActionContext.getContext().getSession();
		CreateRDSO13 createMessage=new CreateRDSO13(releaseDrugs);
		String msg=createMessage.createMessage((String)session.get("facility"));
		message=new HL7Message();
		if(!msg.equals(null)){
			Communicator c=new Communicator();
			String reply=c.sendMessage(msg);
			message.setMessage(msg+"-------------"+reply);
		}
		else{
			message.setMessage("empty");
		}
		return SUCCESS;
	}

	public ReleaseDrugs getReleaseDrugs() {
		return releaseDrugs;
	}

	public void setReleaseDrugs(ReleaseDrugs releaseDrugs) {
		this.releaseDrugs = releaseDrugs;
	}

	public void validate(){
		if(releaseDrugs.getPatientId().length()== 0){
			addFieldError( "releaseDrugs.patientId", "Patient ID required" );
		}
		if(releaseDrugs.getReleaseId().length()== 0){
			addFieldError( "releaseDrugs.releaseId", "Patient ID required" );
		}
		if(releaseDrugs.getDrugId().length()== 0){
			addFieldError( "releaseDrugs.drugId", "Patient ID required" );
		}
		if(releaseDrugs.getQuantity().length()== 0){
			addFieldError( "releaseDrugs.quantity", "Patient ID required" );
		}
		if(releaseDrugs.getUnits().length()== 0){
			addFieldError( "releaseDrugs.units", "Patient ID required" );
		}
		if(releaseDrugs.getUnitOrWard().length()== 0){
			addFieldError( "releaseDrugs.unitOrWard", "Patient ID required" );
		}
		if(releaseDrugs.getReleaseDate()== null){
			addFieldError( "releaseDrugs.releaseDate", "Prescription Date required" );
		}
	}
}
